/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Ringtone Designer 1.0
 *    
 *    RingtoneUtil
 *    TODO File description or class description.
 *
 *    @author: qixiao
 *    @since:  2010-11-20
 *    @version: 1.0
 *
 *****************************************************************************/

package com.sdk.util.phone;


import com.sdk.util.CursorUtil;
import com.sdk.util.Log;
import com.sdk.util.StringUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore.Audio.Media;
import android.provider.Settings;

/**
 * 
 */
public final class RingtoneUtil {
	public static final int ALRAM = 0;
	public static final int RINGTONE = 1;
	public static final int NOTIFYCATION = 2;
	private static final String TAG = RingtoneUtil.class.getSimpleName();
	public static final int SET_RINGTONE_SUCCESS = 1;
	public static final int ERROR_CODE_SET_RINGTONE_FAILED = -1;
	public static final int ERROR_CODE_SD_CARD_NOT_AVAILABLE = -2;
	public static final int ERROR_CODE_SET_RINGTONE_FAILED_FILE_NOT_EXISTED = -3;

	public static void setRingtone(final Context aContext, final int aType,
			final Uri aUri) {
		RingtoneManager.setActualDefaultRingtoneUri(aContext, aType, aUri);
		Log.d(TAG, "type is " + aType + " uri :" + aUri.toString());
	}

	public static int setRingtone(Context mContext, final String filePath,
			int type) {
		if (!StorageHelper.isSdCardAvaliable()) {
			return ERROR_CODE_SD_CARD_NOT_AVAILABLE;
		}
		final String dataPath = filePath;
		final Cursor cursorExternalDb = mContext.getContentResolver().query(
				Media.EXTERNAL_CONTENT_URI, null,
				Media.DATA + " = '" + StringUtil.dbUnescape(dataPath) + "'",
				null, null);
		
		int typeInt = -1;
		final ContentValues values = new ContentValues();
		if (RINGTONE == type) {
			typeInt = RingtoneManager.TYPE_RINGTONE;
			values.put(Media.IS_RINGTONE, 1);
			Log.d(TAG, "I set ringtone");
		} else if (ALRAM == type) {
			typeInt = RingtoneManager.TYPE_ALARM;
			values.put(Media.IS_ALARM, 1);
			Log.d(TAG, "I set alarm");
		} else if (NOTIFYCATION == type) {
			typeInt = RingtoneManager.TYPE_NOTIFICATION;
			values.put(Media.IS_NOTIFICATION, 1);
			Log.d(TAG, "I set notification");
		}
		boolean externalDbFlag = false;
		final boolean mongoDbFlag = false;

		if (CursorUtil.cursorHaveValue(cursorExternalDb)) {
			while (cursorExternalDb.moveToNext()) {
				final int id = cursorExternalDb.getColumnIndex(Media._ID);
				externalDbFlag = mContext.getContentResolver().update(
						Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, ""
								+ cursorExternalDb.getInt(id)), values, null,
						null) > 0;
				// mongoDbFlag = mDao.updateRingtoneType(dataPath, type);
				Log.d(TAG, "externalDbFlag 1: " + externalDbFlag);
				Log.d(TAG, "mongoDbFlag 1: " + mongoDbFlag);
				if (externalDbFlag) {
					RingtoneUtil.setRingtone(
							mContext,
							typeInt,
							Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, ""
									+ cursorExternalDb.getInt(id)));
					return SET_RINGTONE_SUCCESS;
				}
			}
		} else {
			try {
				final Uri uri = saveRingtone(mContext, filePath, type);
				RingtoneUtil.setRingtone(mContext, typeInt, uri);
				return SET_RINGTONE_SUCCESS;
			} catch (final SQLiteConstraintException e) {
				return ERROR_CODE_SET_RINGTONE_FAILED;
			}
		}
		return ERROR_CODE_SET_RINGTONE_FAILED;
	}

	private static Uri saveRingtone(Context mContext, String filePath, int type)
			throws SQLiteConstraintException {
		final ContentValues values = new ContentValues();
		if (RINGTONE == type) {
			values.put(Media.IS_RINGTONE, true);
		} else if (ALRAM == type) {
			values.put(Media.IS_ALARM, true);
		} else if (NOTIFYCATION == type) {
			values.put(Media.IS_NOTIFICATION, true);
		}
		values.put(Media.DATA, StringUtil.dbUnescape(filePath));
		// values.put(Media.ARTIST, StringUtil.dbUnescape(audio.getArtist()));
		// values.put(Media.TITLE, StringUtil.dbUnescape(audio.getTitle()));
		// XXX
		final Uri resultUri = mContext.getContentResolver().insert(
				Media.EXTERNAL_CONTENT_URI, values);

		return resultUri;
	}

	public static void deleteRingtone(Context context, Uri uri) {
		Uri settingUri = RingtoneManager.getActualDefaultRingtoneUri(context,
				RingtoneManager.TYPE_RINGTONE);
		if (uri.equals(settingUri)) {
			updateSettings(context, uri, RingtoneManager.TYPE_RINGTONE);
		}
		settingUri = RingtoneManager.getActualDefaultRingtoneUri(context,
				RingtoneManager.TYPE_ALARM);
		if (uri.equals(settingUri)) {
			updateSettings(context, uri, RingtoneManager.TYPE_ALARM);
		}
		settingUri = RingtoneManager.getActualDefaultRingtoneUri(context,
				RingtoneManager.TYPE_NOTIFICATION);
		if (uri.equals(settingUri)) {
			updateSettings(context, uri, RingtoneManager.TYPE_NOTIFICATION);
		}
	}

	public static void updateSettings(Context context, Uri uri, int type) {
		final ContentValues values = new ContentValues();
		if (type == RingtoneManager.TYPE_RINGTONE) {
			values.put(Settings.System.RINGTONE, "");
			context.getContentResolver().update(
					Settings.System.CONTENT_URI,
					values,
					Settings.NameValueTable.NAME + "='"
							+ Settings.System.RINGTONE + "'", null);
		}
		if (type == RingtoneManager.TYPE_NOTIFICATION) {
			values.put(Settings.System.NOTIFICATION_SOUND, "");
			context.getContentResolver().update(
					Settings.System.CONTENT_URI,
					values,
					Settings.NameValueTable.NAME + "='"
							+ Settings.System.NOTIFICATION_SOUND + "'", null);
		}
		if (type == RingtoneManager.TYPE_ALARM) {
			values.put(Settings.System.ALARM_ALERT, "");
			context.getContentResolver().update(
					Settings.System.CONTENT_URI,
					values,
					Settings.NameValueTable.NAME + "='"
							+ Settings.System.ALARM_ALERT + "'", null);
		}

	}
}
